//
//  UIImage+WebPFormatter.m
//  mmbang
//
//  Created by 倪元政 on 16/8/8.
//  Copyright © 2016年 iyaya. All rights reserved.
//

#import "UIImage+WebPFormatter.h"


@implementation UIImage (WebPFormatter)

#pragma mark - Private methods

//+ (NSData *)convertToWebP:(UIImage *)image
//                  quality:(CGFloat)quality
//                    alpha:(CGFloat)alpha
//                   preset:(WebPPreset)preset
//              configBlock:(void (^)(WebPConfig *))configBlock
//                    error:(NSError **)error
//{
//    if (alpha < 1) {
//        image = [self webPImage:image withAlpha:alpha];
//    }
//    
//    CGImageRef webPImageRef = image.CGImage;
//    size_t webPBytesPerRow = CGImageGetBytesPerRow(webPImageRef);
//    
//    size_t webPImageWidth = CGImageGetWidth(webPImageRef);
//    size_t webPImageHeight = CGImageGetHeight(webPImageRef);
//    
//    CGDataProviderRef webPDataProviderRef = CGImageGetDataProvider(webPImageRef);
//    CFDataRef webPImageDatRef = CGDataProviderCopyData(webPDataProviderRef);
//    
//    uint8_t *webPImageData = (uint8_t *)CFDataGetBytePtr(webPImageDatRef);
//    
//    WebPConfig config;
//    if (!WebPConfigPreset(&config, preset, quality)) {
//        NSMutableDictionary *errorDetail = [NSMutableDictionary dictionary];
//        [errorDetail setValue:@"Configuration preset failed to initialize." forKey:NSLocalizedDescriptionKey];
//        if(error != NULL)
//            *error = [NSError errorWithDomain:[NSString stringWithFormat:@"%@.errorDomain",  [[NSBundle mainBundle] bundleIdentifier]] code:-101 userInfo:errorDetail];
//        
//        CFRelease(webPImageDatRef);
//        return nil;
//    }
//    
//    if (configBlock) {
//        configBlock(&config);
//    }
//    
//    if (!WebPValidateConfig(&config)) {
//        NSMutableDictionary *errorDetail = [NSMutableDictionary dictionary];
//        [errorDetail setValue:@"One or more configuration parameters are beyond their valid ranges." forKey:NSLocalizedDescriptionKey];
//        if(error != NULL)
//            *error = [NSError errorWithDomain:[NSString stringWithFormat:@"%@.errorDomain",  [[NSBundle mainBundle] bundleIdentifier]] code:-101 userInfo:errorDetail];
//        
//        CFRelease(webPImageDatRef);
//        return nil;
//    }
//    
//    WebPPicture pic;
//    if (!WebPPictureInit(&pic)) {
//        NSMutableDictionary *errorDetail = [NSMutableDictionary dictionary];
//        [errorDetail setValue:@"Failed to initialize structure. Version mismatch." forKey:NSLocalizedDescriptionKey];
//        if(error != NULL)
//            *error = [NSError errorWithDomain:[NSString stringWithFormat:@"%@.errorDomain",  [[NSBundle mainBundle] bundleIdentifier]] code:-101 userInfo:errorDetail];
//        
//        CFRelease(webPImageDatRef);
//        return nil;
//        
//        
//        
//    }
//    pic.width = (int)webPImageWidth;
//    pic.height = (int)webPImageHeight;
//    pic.colorspace = WEBP_YUV420;
//    
//    WebPPictureImportRGBA(&pic, webPImageData, (int)webPBytesPerRow);
//    WebPPictureARGBToYUVA(&pic, WEBP_YUV420);
//    WebPCleanupTransparentArea(&pic);
//    
//    WebPMemoryWriter writer;
//    WebPMemoryWriterInit(&writer);
//    pic.writer = WebPMemoryWrite;
//    pic.custom_ptr = &writer;
//    WebPEncode(&config, &pic);
//    
//    NSData *webPFinalData = [NSData dataWithBytes:writer.mem length:writer.size];
//    
//    free(writer.mem);
//    WebPPictureFree(&pic);
//    CFRelease(webPImageDatRef);
//    
//    return webPFinalData;
//}
//
//
//#pragma mark - Synchronous methods
//
//+ (NSData *)imageToWebP:(UIImage *)image quality:(CGFloat)quality
//{
//    NSParameterAssert(image != nil);
//    NSParameterAssert(quality >= 0.0f && quality <= 100.0f);
//    return [self convertToWebP:image quality:quality alpha:1.0f preset:WEBP_PRESET_DEFAULT configBlock:nil error:nil];
//}
//
//+ (void)imageToWebP:(UIImage *)image
//            quality:(CGFloat)quality
//              alpha:(CGFloat)alpha
//             preset:(WebPPreset)preset
//    completionBlock:(void (^)(NSData *result))completionBlock
//       failureBlock:(void (^)(NSError *error))failureBlock
//{
//    [self imageToWebP:image
//              quality:quality
//                alpha:alpha
//               preset:preset
//          configBlock:nil
//      completionBlock:completionBlock
//         failureBlock:failureBlock];
//}
//
//+ (void)imageToWebP:(UIImage *)image
//            quality:(CGFloat)quality
//              alpha:(CGFloat)alpha
//             preset:(WebPPreset)preset
//        configBlock:(void (^)(WebPConfig *))configBlock
//    completionBlock:(void (^)(NSData *result))completionBlock
//       failureBlock:(void (^)(NSError *error))failureBlock
//{
//    NSAssert(image != nil, @"imageToWebP:quality:alpha:completionBlock:failureBlock image cannot be nil");
//    NSAssert(quality >= 0 && quality <= 100, @"imageToWebP:quality:alpha:completionBlock:failureBlock quality has to be [0, 100]");
//    NSAssert(alpha >= 0 && alpha <= 1, @"imageToWebP:quality:alpha:completionBlock:failureBlock alpha has to be [0, 1]");
//    NSAssert(completionBlock != nil, @"imageToWebP:quality:alpha:completionBlock:failureBlock completionBlock cannot be nil");
//    NSAssert(failureBlock != nil, @"imageToWebP:quality:alpha:completionBlock:failureBlock failureBlock block cannot be nil");
//    
//    // Create dispatch_queue_t for encoding WebP concurrently
//    dispatch_queue_t toWebPQueue = dispatch_queue_create("com.seanooi.ioswebp.towebp", DISPATCH_QUEUE_CONCURRENT);
//    dispatch_async(toWebPQueue, ^{
//        
//        NSError *error = nil;
//        NSData *webPFinalData = [self convertToWebP:image quality:quality alpha:alpha preset:preset configBlock:configBlock error:&error];
//        
//        // Return results to caller on main thread in completion block is `webPFinalData` != nil
//        // Else return in failure block
//        if(!error) {
//            dispatch_async(dispatch_get_main_queue(), ^{
//                completionBlock(webPFinalData);
//            });
//        }
//        else {
//            dispatch_async(dispatch_get_main_queue(), ^{
//                failureBlock(error);
//            });
//        }
//    });
//}
//
//#pragma mark - Utilities
//
//- (UIImage *)imageByApplyingAlpha:(CGFloat) alpha
//{
//    NSParameterAssert(alpha >= 0.0f && alpha <= 1.0f);
//    
//    if (alpha < 1) {
//        
//        UIGraphicsBeginImageContextWithOptions(self.size, NO, 0.0f);
//        
//        CGContextRef ctx = UIGraphicsGetCurrentContext();
//        CGRect area = CGRectMake(0, 0, self.size.width, self.size.height);
//        
//        CGContextScaleCTM(ctx, 1, -1);
//        CGContextTranslateCTM(ctx, 0, -area.size.height);
//        
//        CGContextSetAlpha(ctx, alpha);
//        CGContextSetBlendMode(ctx, kCGBlendModeXOR);
//        CGContextSetFillColorWithColor(ctx, [UIColor clearColor].CGColor);
//        
//        CGContextDrawImage(ctx, area, self.CGImage);
//        
//        UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
//        
//        UIGraphicsEndImageContext();
//        
//        return newImage;
//        
//    }
//    else {
//        return self;
//    }
//}
//
//+ (UIImage *)webPImage:(UIImage *)image withAlpha:(CGFloat)alpha
//{
//    // CGImageAlphaInfo of images with alpha are kCGImageAlphaPremultipliedFirst
//    // Convert to kCGImageAlphaPremultipliedLast to avoid gray-ish background
//    // when encoding alpha images to WebP format
//    
//    CGImageRef imageRef = image.CGImage;
//    NSUInteger width = CGImageGetWidth(imageRef);
//    NSUInteger height = CGImageGetHeight(imageRef);
//    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
//    
//    UInt8* pixelBuffer = malloc(height * width * 4);
//    
//    NSUInteger bytesPerPixel = 4;
//    NSUInteger bytesPerRow = bytesPerPixel * width;
//    NSUInteger bitsPerComponent = 8;
//    
//    CGContextRef context = CGBitmapContextCreate(pixelBuffer, width, height, bitsPerComponent, bytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
//    CGContextSetRGBFillColor(context, 0, 0, 0, 1);
//    CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
//    CGContextRelease(context);
//    
//    CGDataProviderRef dataProviderRef = CGImageGetDataProvider(imageRef);
//    CFDataRef dataRef = CGDataProviderCopyData(dataProviderRef);
//    
//    GLubyte *pixels = (GLubyte *)CFDataGetBytePtr(dataRef);
//    
//    for (int y = 0; y < height; ++y) {
//        for (int x = 0; x < width; ++x) {
//            NSInteger byteIndex = ((width * 4) * y) + (x * 4);
//            pixelBuffer[byteIndex + 3] = pixels[byteIndex +3 ]*alpha;
//        }
//    }
//    
//    CGContextRef ctx = CGBitmapContextCreate(pixelBuffer, width, height, bitsPerComponent, bytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
//    CGImageRef newImgRef = CGBitmapContextCreateImage(ctx);
//    
//    free(pixelBuffer);
//    CFRelease(dataRef);
//    CGColorSpaceRelease(colorSpace);
//    CGContextRelease(ctx);
//    
//    UIImage *newImage = [UIImage imageWithCGImage:newImgRef];
//    CGImageRelease(newImgRef);
//    
//    return newImage;
//}
//
//+ (NSString *)version:(NSInteger)version
//{
//    // Convert version number to hexadecimal and parse it accordingly
//    // E.g: v2.5.7 is 0x020507
//    
//    NSString *hex = [NSString stringWithFormat:@"%06lx", (long)version];
//    NSMutableArray *array = [NSMutableArray array];
//    for (int x = 0; x < [hex length]; x += 2) {
//        [array addObject:@([[hex substringWithRange:NSMakeRange(x, 2)] integerValue])];
//    }
//    
//    return [array componentsJoinedByString:@"."];
//}


@end
